/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xos_cat.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../data_struct/head.h"
#include "../config/head.h"
#include "../macro/head.h"
#include "../impl/head.h"
#include "../msg/head.h"
#include "helper_heart.h"
#include "helper_main.h"

namespace xos_cat
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    helper_heart::helper_heart()
    {
    }

    helper_heart::~helper_heart()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // static

    int helper_heart::init()
    {
        int ret = 0;

        xos_box::i_heart * pHeart = mgr::container()->box()->heart();

        if( pHeart )
        {
            pHeart->add_acceptor( on_heart, config::get()->heart_check_interval_ms );
            pHeart->set_sys_check_interval( config::get()->sys_heart_interval_ms );
            pHeart->start_heart_thread();
        }

        return ret;
    }

    int helper_heart::term()
    {
        int ret = 0;

        xos_box::i_heart* pHeart = mgr::container()->box()->heart();

        if( pHeart )
        {
            pHeart->stop_heart_thread();
            pHeart->rem_acceptor( helper_heart::on_heart );
            pHeart->set_sys_check_interval( 10000 );
        }

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // 
    // 消息响回调函数
    // 
    int helper_heart::on_heart()
    {
        int ret = 0;

        if( !state::running() )
        {
            return 0;
        }

        helper_main::get()->notify( MAIN_HEART );

        return ret;
    }

} // xos_cat
